package com.niit.users.service.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.niit.common.pojo.ApiResponse;
import com.niit.common.pojo.ApiResponseEnum;
import com.niit.common.pojo.JSONEnum;
import com.niit.common.service.impl.BaseServiceImpl;
import com.niit.common.util.ConstatFinalUtil;
import com.niit.common.util.PageInfoUtil;
import com.niit.users.dao.ITbManangerDao;
import com.niit.users.dao.ITbPassengerDao;
import com.niit.users.dao.impl.TbManangerDaoImpl;
import com.niit.users.dao.impl.TbPassengerDaoImpl;
import com.niit.users.pojo.TbMananger;
import com.niit.users.pojo.TbPassenger;
import com.niit.users.service.IUsersDbService;

/**
 * usersService的实现类 Service需要通过读写器赋值
 *
 * @author wangshMac
 */
public class UsersDbServiceImpl extends BaseServiceImpl implements IUsersDbService {
    private ITbManangerDao manangerDao = new TbManangerDaoImpl();
    private ITbPassengerDao passengerDao = new TbPassengerDaoImpl();

    @Override
    public ApiResponse<Object> saveOneManangerService(TbMananger pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        if (Objects.isNull(pojo.getManangerName()) || "".equalsIgnoreCase(pojo.getManangerName())) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }
        /* 根据名称查询 */
        Map<String,Object> condMap = new HashMap<String, Object>();
        condMap.put("manangerName", pojo.getManangerName());
        ApiResponse<TbMananger> dbApiResponse = this.findOneManangerService(condMap);
        TbMananger dbDataOne = dbApiResponse.getDataOneJava();
        if (Objects.nonNull(dbDataOne)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        int res = this.manangerDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getManagerId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOneManangerService(TbMananger pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.manangerDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getManagerId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOneManangerService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.manangerDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbMananger> findCondListManangerService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbMananger> apiResponse = new ApiResponse<TbMananger>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<TbMananger> demoTbManangerList = Collections.EMPTY_LIST;

        Map<String, List<TbMananger>> dataList = new HashMap<String, List<TbMananger>>();
        demoTbManangerList = this.manangerDao.findCondList(pageInfoUtil, condMap);
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoTbManangerList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoTbManangerList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbMananger> findOneManangerService(Map<String, Object> condMap) {
        ApiResponse<TbMananger> apiResponse = new ApiResponse<TbMananger>();

        Map<String, TbMananger> data = new HashMap<String, TbMananger>();
        TbMananger pojo = this.manangerDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
    
    @Override
    public ApiResponse<Object> saveOnePassengerService(TbPassenger pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();

        if (Objects.isNull(pojo.getPassengerName()) || "".equalsIgnoreCase(pojo.getPassengerName())) {
            apiResponse.setCode(ApiResponseEnum.INFO_EMPTY.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }
        /* 根据名称查询 */
        Map<String,Object> condMap = new HashMap<String, Object>();
        condMap.put("passengerName", pojo.getPassengerName());
        ApiResponse<TbPassenger> dbApiResponse = this.findOnePassengerService(condMap);
        TbPassenger dbDataOne = dbApiResponse.getDataOneJava();
        if (Objects.nonNull(dbDataOne)) {
            apiResponse.setCode(ApiResponseEnum.INFO_RECORD_EXISTS.getStatus());
            apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
            return apiResponse;
        }
        
        int res = this.passengerDao.saveOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getPassengerId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> updateOnePassengerService(TbPassenger pojo) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.passengerDao.updateOne(pojo);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            dataMap.put(JSONEnum.ID.getName(), pojo.getPassengerId());
            dataMap.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<Object> deleteOnePassengerService(Map<String, Object> condMap) {
        ApiResponse<Object> apiResponse = new ApiResponse<Object>();
        int res = this.passengerDao.deleteOne(condMap);
        if (res > 0) {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            dataMap.put(JSONEnum.EFFECT.getName(), res);
            apiResponse.setDataOne(dataMap);

            apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        } else {
            apiResponse.setCode(ApiResponseEnum.STATUS_FAILED.getStatus());
        }
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbPassenger> findCondListPassengerService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        ApiResponse<TbPassenger> apiResponse = new ApiResponse<TbPassenger>();
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        List<TbPassenger> demoTbPassengerList = Collections.EMPTY_LIST;

        Map<String, List<TbPassenger>> dataList = new HashMap<String, List<TbPassenger>>();
        demoTbPassengerList = this.passengerDao.findCondList(pageInfoUtil, condMap);
        /* 将结果放到JSON中 */
        dataList.put(ApiResponseEnum.NAME_LIST.getName(), demoTbPassengerList);

        apiResponse.setPageInfoUtil(pageInfoUtil);
        apiResponse.setDataListJava(demoTbPassengerList);
        apiResponse.setDataList(dataList);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }

    @Override
    public ApiResponse<TbPassenger> findOnePassengerService(Map<String, Object> condMap) {
        ApiResponse<TbPassenger> apiResponse = new ApiResponse<TbPassenger>();

        Map<String, TbPassenger> data = new HashMap<String, TbPassenger>();
        TbPassenger pojo = this.passengerDao.findOne(condMap);
        data.put(ApiResponseEnum.NAME_ONE.getName(), pojo);
        apiResponse.setCode(ApiResponseEnum.STATUS_SUCCESS.getStatus());
        /* 将结果放到JSON中 */
        apiResponse.setDataOne(data);
        apiResponse.setDataOneJava(pojo);
        apiResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponse.getCode() + ""), Collections.EMPTY_MAP);
        return apiResponse;
    }
}
